package com.ruoyi.common.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

public class CallPythonUtils {
//    public static String pythonExePath = "C:\\Program Files\\Python310\\python.exe";
    public static String pythonExePath = "D:\\pythonProject\\Demo\\venv\\Scripts\\python.exe";
    public static String pythonFileRootPath= "D:\\pythonProject\\DemoFile\\";
//    public static String pythonFileRootPath= "D:\\";

    public static Map<String,String> CallPythonFile(String filePath, String jsonParam){
        Map<String,String> map = new HashMap<>();
        new Thread(()->{
            String outputData = "";
            ProcessBuilder pb = null;
            Process process = null;
            try {
                // 创建进程构建器并指定要执行的命令
//            pb = new ProcessBuilder("C:\\Program Files\\Python310\\python.exe", "D:\\FilePath.py");
                pb = new ProcessBuilder(pythonExePath, pythonFileRootPath+filePath);
                // 启动进程
                process = pb.start();
                // 获取Python进程的标准输入流
                OutputStreamWriter inputWriter = new OutputStreamWriter(process.getOutputStream());
                // 获取Python进程的标准输出流
                BufferedReader outputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

                inputWriter.write(jsonParam);
                inputWriter.flush();
                inputWriter.close();
                // 读取Python进程的输出
                ;
                while ((outputData = outputReader.readLine())!=null){
                    System.out.println(outputData);
                }
                // 关闭输入输出流
                outputReader.close();
                // 等待Python进程结束
                int exitCode = process.waitFor();

                // 输出Python进程的返回值和输出
                System.out.println("Exit Code: " + exitCode);
                System.out.println("Output: " + outputData);
                map.put("ExitCode",exitCode+"");
                map.put("Output",outputData);
            } catch (Exception  e) {
                map.put("ExitCode","1");
                e.printStackTrace();
            }finally {
                if(process!=null){
                    process.destroyForcibly();
                }
            }
        }).start();

        return map;
    }

    public static void main(String[] args) {

        Process proc;
        try {
            String[] args1 = new String[] { pythonExePath, "D:\\pythonProject\\DemoFile\\single.py.py", "E:/jadite/test/operate\\merge\\31813_姿.csv", String.valueOf(2) };

            proc = Runtime.getRuntime().exec(args1);
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            proc.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static String executeCmd(String cmd) {
        try {
            String cmdBin = "cmd /c ";
            Process process = Runtime.getRuntime().exec(cmdBin + cmd);
            String result = getExecResult(process);
            return result;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static String getExecResult(Process process) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName(getsystemLanguage())));
        String result = "";
        String line;
        while ((line = br.readLine()) != null) {
            result += line;
        }
        return result;
    }

    private static String getsystemLanguage() {
        return null == System.getProperty("sun.jnu.encoding") ? "GBK"
                : System.getProperty("sun.jnu.encoding");
    }
}
